package com.mia.props.common.entities;

import com.mia.props.Props;
import com.mia.props.common.TileProps;
import java.util.Random;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.world.World;

public class TileContainer extends TileProps implements IInventory {
   protected ItemStack[] slots = new ItemStack[128];
   private Random rnd = new Random();

   public boolean onBlockActivated(World world, int x, int y, int z, EntityPlayer player, int side, float var7, float var8, float var9) {
      if (!world.isRemote) {
         player.openGui(Props.instance, 0, world, x, y, z);
      }

      return true;
   }

   public int getSizeInventory() {
      return this.getSizeInventoryX() * this.getSizeInventoryY();
   }

   public ItemStack getStackInSlot(int var1) {
      return this.slots[var1];
   }

   public ItemStack decrStackSize(int slot, int amount) {
      if (this.slots[slot] != null) {
         ItemStack stack;
         if (this.slots[slot].stackSize <= amount) {
            stack = this.slots[slot];
            this.slots[slot] = null;
            this.markDirty();
            return stack;
         } else {
            stack = this.slots[slot].splitStack(amount);
            if (this.slots[slot].stackSize == 0) {
               this.slots[slot] = null;
            }

            this.markDirty();
            return stack;
         }
      } else {
         return null;
      }
   }

   public ItemStack getStackInSlotOnClosing(int slot) {
      if (this.slots[slot] != null) {
         ItemStack stack = this.slots[slot];
         this.slots[slot] = null;
         return stack;
      } else {
         return null;
      }
   }

   public void setInventorySlotContents(int slot, ItemStack stack) {
      this.slots[slot] = stack;
      if (stack != null && stack.stackSize > this.getInventoryStackLimit()) {
         stack.stackSize = this.getInventoryStackLimit();
      }

      this.markDirty();
   }

   public String getInventoryName() {
      return "decocraft.tilecontainer";
   }

   public boolean hasCustomInventoryName() {
      return false;
   }

   public int getInventoryStackLimit() {
      return 64;
   }

   public boolean isUseableByPlayer(EntityPlayer player) {
      return this.worldObj.getTileEntity(this.xCoord, this.yCoord, this.zCoord) != this ? false : player.getDistanceSq((double)this.xCoord + 0.5D, (double)this.yCoord + 0.5D, (double)this.zCoord + 0.5D) <= 64.0D;
   }

   public void openInventory() {
   }

   public void closeInventory() {
   }

   public boolean isItemValidForSlot(int slot, ItemStack stack) {
      if (this.slots[slot] == null) {
         return true;
      } else {
         return this.slots[slot].isItemEqual(stack);
      }
   }

   public void readFromNBT(NBTTagCompound tagCompound) {
      super.readFromNBT(tagCompound);
      NBTTagList stackList = tagCompound.getTagList("Items", 10);
      this.slots = new ItemStack[this.getSizeInventory()];

      for(int i = 0; i < stackList.tagCount(); ++i) {
         NBTTagCompound stackTag = stackList.getCompoundTagAt(i);
         int islot = stackTag.getByte("Slot") & 255;
         if (islot >= 0 && islot < this.slots.length) {
            this.slots[islot] = ItemStack.loadItemStackFromNBT(stackTag);
         }
      }

   }

   public void writeToNBT(NBTTagCompound tagCompound) {
      super.writeToNBT(tagCompound);
      NBTTagList nbttaglist = new NBTTagList();

      for(int i = 0; i < this.slots.length; ++i) {
         if (this.slots[i] != null) {
            NBTTagCompound nbttagcompound1 = new NBTTagCompound();
            nbttagcompound1.setByte("Slot", (byte)i);
            this.slots[i].writeToNBT(nbttagcompound1);
            nbttaglist.appendTag(nbttagcompound1);
         }
      }

      tagCompound.setTag("Items", nbttaglist);
   }

   public void onBlockHarvested(World world, int x, int y, int z, int par5, EntityPlayer par6EntityPlayer) {
      for(int i1 = 0; i1 < this.getSizeInventory(); ++i1) {
         ItemStack itemstack = this.getStackInSlot(i1);
         if (itemstack != null) {
            float f = this.rnd.nextFloat() * 0.8F + 0.1F;
            float f1 = this.rnd.nextFloat() * 0.8F + 0.1F;

            EntityItem entityitem;
            for(float f2 = this.rnd.nextFloat() * 0.8F + 0.1F; itemstack.stackSize > 0; world.spawnEntityInWorld(entityitem)) {
               int j1 = this.rnd.nextInt(21) + 10;
               if (j1 > itemstack.stackSize) {
                  j1 = itemstack.stackSize;
               }

               itemstack.stackSize -= j1;
               entityitem = new EntityItem(world, (double)((float)x + f), (double)((float)y + f1), (double)((float)z + f2), new ItemStack(itemstack.getItem(), j1, itemstack.getItemDamage()));
               float f3 = 0.05F;
               entityitem.motionX = (double)((float)this.rnd.nextGaussian() * f3);
               entityitem.motionY = (double)((float)this.rnd.nextGaussian() * f3 + 0.2F);
               entityitem.motionZ = (double)((float)this.rnd.nextGaussian() * f3);
               if (itemstack.hasTagCompound()) {
                  entityitem.getEntityItem().setTagCompound((NBTTagCompound)itemstack.getTagCompound().copy());
               }
            }
         }
      }

   }
}
